<?php

class LinkController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/home';
	
	protected function beforeAction($action){
		if(Utils::isAdmin(Yii::app()->user->id)){
			throw  new CHttpException("403","Access Denied");
		}
		return true;
	}

	public function actionAdd(){
		$model = new Link();
		$model->url="url";
		$model->logo="logo";
		$model->title = "title";
		$model->seq=100;
		$model->save();
		$this->redirect(array("link/admin"));
	}
	public function actionDelete($id){
		Link::model()->deleteByPk($id);
		$this->redirect(array("link/admin"));
	}

	public function actionAdmin()
	{
		if(isset($_POST["Link"])){
			foreach($_POST["Link"] as $modelArr){
				$model = Link::model()->findByPk($modelArr['id']);
				$model->attributes=$modelArr;
				if(!$model->save()){
					echo $model;exit;
				}
			}
		}
		$criteria = new CDbCriteria;
		$criteria->order = '`seq` asc';
		$models = Link::model()->findAll($criteria);
		$this->render('admin',array("models"=>$models));
	}
}
